# Build recipe for noweb
#
# Copyright 2017 Mateus P. Rodrigues <mprodrigues@dragora.org>.
#
# This recipe is free software, under the terms of the Apache License 2.0

program=noweb
version=2.12
release=1

description="
noweb is a simple literate programming tool similar to WEB
"

homepage=https://www.cs.tufts.edu/~nr/noweb/
license=custom

tarname=master.tar.gz

# Remote source(s)
fetch="https://github.com/nrnrnr/noweb/archive/$tarname"

# Source documentation
docs="CHANGES COPYRIGHT README"

pkgname=noweb
pkgversion=2.12
srcdir=noweb-master

build() {
    unpack "${tardir}/$tarname"

    cd "$srcdir"/src

    sed -i -e 's+BIN=/usr/local/noweb+BIN=\$\(DESTDIR\)/usr/bin+' \
    -e 's+LIB=/usr/local/noweb/lib+LIB=\$\(DESTDIR\)/usr/lib$\{libSuffix\}+' \
    -e 's+MAN=/usr/local/noweb/man+MAN=\$\(DESTDIR\)/usr/share/man+' \
    -e 's+/usr/local/tex/inputs+\$\(DESTDIR\)/usr/local/texlive/texmf-local/tex/plain/local+' \
    -e 's+$(HOME)/www/noweb/FAQ.html+'$srcdir/$pkgname-$pkgver'/src/FAQ.html+' \
    -e 's+mkdir+install -d+' -e 's+> \$\(BIN\)+> \$destdir/bin/+' Makefile

    make boot
    make DESTDIR="$destdir" LIBSRC=icon ICONC=icont
    ./awkname gawk
    make DESTDIR=$destdir LIBSRC=icon ICONC=icont install
    find $destdir -type f -exec sed -i "s+${destdir}+/+g" '{}' ';'

    # Compress manual pages
    if [ -d "${destdir}/$mandir" ] ; then
	(
            cd "${destdir}/$mandir"
	    find . -type f -exec lzip -9 '{}' +
	    find . -type l | while read -r file
	    do
		ln -sf "$(readlink -- "$file").lz" "${file}.lz"
		rm -- "$file"
	    done
	)
    fi

    cd ..

    # Copy documentation
    mkdir -p "${destdir}/usr/share/doc/${program}-${version}"
    cp -p $docs "${destdir}/usr/share/doc/${program}-${version}/"
}
